import { Injectable } from '@angular/core';
import { Router }  from '@angular/router';
import { HttpRequest } from "./httpRequest";

@Injectable()
export class LoginService {

  constructor(
    private _router: Router,
    private httpRequest:HttpRequest
  ){}
  public login(user:Object):void{

    this.httpRequest.sendPost('account/login',user,'op').subscribe(res=>{
      if(res.resultStatus == 200){
        localStorage.setItem("isLogin",'true');
        localStorage.setItem("token",res.result.token);
        localStorage.setItem("account",JSON.stringify(res.result.account));
        let menus = [];
        res.result.menus.forEach(el=>{
          menus.push(el.name);
        });
        localStorage.setItem("menus",JSON.stringify(menus));
        this._router.navigateByUrl('/page');
      }
    });

  }

  public logout():void{ 
    localStorage.clear();
        //this._router.navigateByUrl('/login');
        window.location.href='/login';


  }



}
